package guda.house.biz.helper.weixin.form;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by well on 15/5/11.
 */
public class WeixinRequest {

    private String appid;
    //设备ID 可空
    private String device_info;
    private String mch_id;
    //随机字符串 不超过32
    private String nonce_str;
    private String sign;
    //商品描述
    private String body;
    //商品详情 可空
    private String detail;
    private String attach;
    private String fee_type;

    private String time_start;
    private String time_expire;
    private String goods_tag;
    private String product_id;


    private String notify_url;
    private String openid;
    private String out_trade_no;
    private String spbill_create_ip;
    private String total_fee;
    private String trade_type = "NATIVE";

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getDevice_info() {
        return device_info;
    }

    public void setDevice_info(String device_info) {
        this.device_info = device_info;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getAttach() {
        return attach;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public String getFee_type() {
        return fee_type;
    }

    public void setFee_type(String fee_type) {
        this.fee_type = fee_type;
    }

    public String getTime_start() {
        return time_start;
    }

    public void setTime_start(String time_start) {
        this.time_start = time_start;
    }

    public String getTime_expire() {
        return time_expire;
    }

    public void setTime_expire(String time_expire) {
        this.time_expire = time_expire;
    }

    public String getGoods_tag() {
        return goods_tag;
    }

    public void setGoods_tag(String goods_tag) {
        this.goods_tag = goods_tag;
    }

    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getSpbill_create_ip() {
        return spbill_create_ip;
    }

    public void setSpbill_create_ip(String spbill_create_ip) {
        this.spbill_create_ip = spbill_create_ip;
    }

    public String getTotal_fee() {
        return total_fee;
    }

    public void setTotal_fee(String total_fee) {
        this.total_fee = total_fee;
    }

    public String getTrade_type() {
        return trade_type;
    }

    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }

    public String toXML(){
        final StringBuilder buf = new StringBuilder();
        buf.append("<xml>");
        final Object current = this;
        ReflectionUtils.doWithFields(this.getClass(), new ReflectionUtils.FieldCallback() {
            @Override
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                field.setAccessible(true);
                String name = field.getName();
                Object o = field.get(current);
                if(o!= null){
                    buf.append("<").append(name).append("><![CDATA[").append(String.valueOf(o)).append("]]></").append(name).append(">");
                }
            }
        });
        buf.append("</xml>");
        System.out.println(buf.toString());
        return buf.toString();
    }

    public Map<String,String> toParamMap(){
        final Map<String,String> param = new HashMap<String, String>();
        final Object current = this;
        ReflectionUtils.doWithFields(this.getClass(), new ReflectionUtils.FieldCallback() {
            @Override
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                field.setAccessible(true);
                String name = field.getName();
                Object o = field.get(current);
                if(o!= null){
                    param.put(name, String.valueOf(o));
                }
            }
        });
        return param;
    }

    public static void main(String[] args){
        WeixinRequest weixinRequest = new WeixinRequest();
        weixinRequest.setAppid("aaad12");
        System.out.println(weixinRequest.toXML());
    }
}
